var preClass = 'imgNew13Obj_ '

/**
 *
 * @param {*} className 父元素class
 * @param {*} p 父元素的宽高
 * @param {*} s 图片的宽高
 * @param {*} options 其他参数
 */
function newImg13(className, p, s, options) {
  if (!className) {
    console.warn('父元素class不存在，初始化失败')
    return
  }
  if (isEmptyObject(p)) {
    console.warn('父元素宽高不存在，初始化失败')
    return
  }
  if (isEmptyObject(s)) {
    console.warn('图片元素宽高不存在，初始化失败')
    return
  }

  var imgNew13Obj = {
    _container: $('.' + className + ' .newimg13-container'),
    slideActiveIndex: 0, // 轮播图下标  从1开始
    slides: $('.' + className + ' .slide-item'), // 轮播图item  dom集合
    dots: $('.' + className + ' .dot-item'), // 图例item
    arrows: $('.' + className + ' .arrow'), // 方向按钮

    marginLeft: (p.w - s.w * 2) / 4, // 计算图片的marginleft
    parentRect: p,
    imgRect: s,

    timer: undefined, // 轮播图 计时器
    options: options // 传入的其他事件
  }

  $('.' + className + ' .slide-item img').css({
    width: s.w,
    height: s.h
  })

  $('.' + className + ' .slide-item .header').css({
    height: p.h - s.h - 10,
    lineHeight: p.h - s.h - 10 + 'px',
    width: s.w
  })

  // var marginLeft = (p.w - s.w * 2) / 4
  imgNew13Obj.slides.css('margin', '0 ' + imgNew13Obj.marginLeft + 'px')
  // 修改轮播区域元素宽度
  imgNew13Obj._container.css({
    width: (s.w + imgNew13Obj.marginLeft * 2) * (imgNew13Obj.slides.length + 2),
    height: p.h
  })
  // 修改父区域元素宽度
  imgNew13Obj._container.parent().css({
    width: p.w,
    height: p.h
  })

  var last = $('.' + className + ' .slide-item:last').clone()
  var first = $('.' + className + ' .slide-item:first').clone()
  // 最前面添加一个最后的
  // imgNew13Obj._container.prepend($('.' + className + ' .slide-item:last'))
  last.prependTo(imgNew13Obj._container)
  last.css({
    left: -(s.w + imgNew13Obj.marginLeft * 2),
    position: 'absolute'
  })

  // 最后面添加一个第一个
  // imgNew13Obj._container.append($('.' + className + ' .slide-item:first'))
  first.appendTo(imgNew13Obj._container)

  // 方向点击切换图片事件
  imgNew13Obj.arrows.click(function () {
    var index = $(this).data('arrow')
    console.log('index: ', index)
    if (index == 1) {
      next(imgNew13Obj)
    } else if (index == -1) {
      prev(imgNew13Obj)
    }
    console.log('arrow 方向点击事件')
  })

  window[preClass + className] = imgNew13Obj
  window['aaa'] = imgNew13Obj
  moveTo(imgNew13Obj, 0)
}

function moveTo(imgNew13Obj, index) {
  console.log('index: ', index)
  imgNew13Obj.slideActiveIndex = index
  startMove(imgNew13Obj._container, { left: getLeft(imgNew13Obj) })
  stop(imgNew13Obj)
  run(imgNew13Obj)
}
function next(imgNew13Obj) {
  if (imgNew13Obj.slideActiveIndex + 1 === imgNew13Obj.slides.length) {
    console.log('无缝')
    imgNew13Obj.slideActiveIndex = -1
    imgNew13Obj._container.css('left', getLeft(imgNew13Obj) + 'px')
    imgNew13Obj._container.height() // 强制渲染
    moveTo(imgNew13Obj, 0)
  } else {
    moveTo(imgNew13Obj, imgNew13Obj.slideActiveIndex + 1)
  }
}

function prev(imgNew13Obj) {
  if (imgNew13Obj.slideActiveIndex === 0) {
    console.log('无缝')
    imgNew13Obj.slideActiveIndex = imgNew13Obj.slides.length
    imgNew13Obj._container.css('left', getLeft(imgNew13Obj) + 'px')
    imgNew13Obj._container.height() // 强制渲染
    moveTo(imgNew13Obj, imgNew13Obj.slides.length - 1)
  } else {
    moveTo(imgNew13Obj, imgNew13Obj.slideActiveIndex - 1)
  }
}
function stop(imgNew13Obj) {
  clearTimeout(imgNew13Obj.timer)
}

function run(imgNew13Obj) {
  imgNew13Obj.timer = setTimeout(function () {
    next(imgNew13Obj)
  }, 4000)
}

// -------------------utils

//运动框架
function startMove(obj, json, fn) {
  //1. 先清除上一次的计时器
  clearInterval(obj.timer)
  //2. 开启新的计时器
  obj.timer = setInterval(function () {
    //1. 设置开关
    var flag = true
    //2. 遍历json对象
    for (var attr in json) {
      //1. 获取当前属性的值
      var cur = null
      if (attr === 'opacity') {
        cur = Math.floor(parseFloat($(obj).css('opacity')) / 100)
      } else {
        // cur = parseInt(getStyle(obj, attr))
        cur = parseFloat($(obj).css(attr)) || 0
      }

      //2. 计算速度
      var speed = (json[attr] - cur) / 8
      speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
      //3. 检测属性是否达成目标
      if (Math.abs(cur - json[attr]) > 3) {
        flag = false
      }
      //4. 设置运动
      if (attr === 'opacity') {
        // obj.style.opacity = (cur + speed) / 100
        // obj.style.filter = 'alpha(opacity=' + (cur + speed) + ')'
        $(obj).css('opacity', (cur + speed) / 100)
        $(obj).css('filter', 'alpha(opacity=' + (cur + speed) + ')')
      } else {
        // obj.style[attr] = cur + speed + 'px'
        $(obj).css(attr, cur + speed + 'px')
      }
    }
    //3. 检测开关，退出计时器
    if (flag) {
      clearInterval(obj.timer)
      if (fn instanceof Function) {
        fn()
      }
    }
  }, 30)
}
/**
 *
 * @param {*} imgNew13Obj 当前轮播图对象
 * @param {*} imgWidth 图片的宽度 前台传入的
 * @returns 计算轮播图的css left 大小
 */
function getLeft(imgNew13Obj) {
  return -1 * ((imgNew13Obj.imgRect.w + imgNew13Obj.marginLeft * 2) * (imgNew13Obj.slideActiveIndex - 1) + imgNew13Obj.imgRect.w / 2 + imgNew13Obj.marginLeft)
}

function isEmptyObject(obj) {
  if (typeof obj !== 'object' || Object.prototype.toString.call(obj) != '[object Object]') {
    return false
  }

  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      return false
    }
  }

  return true
}
